package com.hnhd.eorder.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.hnhd.eorder.R;
import com.hnhd.eorder.vo.User;

/**
 * 登陆对话框 可以实现服务员登陆
 * 
 * @author dengbiao
 * 
 */
public class LoginDialog extends Dialog {

	/**
	 * 用户列表的listview
	 */
	public ListView lv_userList;

	/**
	 * 列表填充用的键值对的键数组
	 */
	public String[] from = { "userName", "radioButton"};

	/**
	 * 要填充的对象id数组
	 */
	public int[] to = { R.id.tv_login_username, R.id.img_login_selected };
	
	/**
	 * 当前选择的用户位置   --标记用
	 */
	public int checkedPosition = -1;

	public LoginDialog(Context context, int theme) {
		super(context, theme);
		setContentView(R.layout.dialog_login);

		// 获得用户列表对象 并添加监听
		lv_userList = (ListView) findViewById(R.id.user_list);
		this.initListView(lv_userList, from, to);
		lv_userList.setOnItemClickListener(new MyListViewClickListener());

		// 返回按钮 点击关闭登陆对话框
		Button btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				LoginDialog.this.cancel();
			}

		});

	}

	/**
	 * 初始化 listview数组
	 * @param listView 要初始化的listview
	 * @param from 键值对数组
	 * @param to 要填充的id数组
	 */
	@SuppressLint("ParserError")
	private void initListView(ListView listView, String[] from, int[] to) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		ArrayList<User> users = new ArrayList<User>();
		User user = new User("0001","admin");
		users.add(user);
		user = new User("0002","叶子");
		users.add(user);
		user = new User("0003","沙悟净");
		users.add(user);
		user = new User("0004","猪八戒");
		users.add(user);
		user = new User("0005","唐僧");
		users.add(user);
		user = new User("0006","孙悟空");
		users.add(user);
		user = new User("0007","孙尚香");
		users.add(user);
		user = new User("0008","小李飞刀");
		users.add(user);
		user = new User("0009","罗永浩");
		users.add(user);
		user = new User("0010","方舟子");
		users.add(user);
		
		
		for (int i = 0; i < users.size(); i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			user = users.get(i);
			map.put(from[0], user.getUserName());
			map.put(from[1], R.drawable.radio_unchecked);
			map.put("userCode", user.getUserCode());
			list.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(this.getContext(), list,
				R.layout.item_login_user, from, to);
		listView.setAdapter(adapter);
	}

	/**
	 * ListView监听类   处理listview中item被点击事件
	 * @author dengbiao
	 *
	 */
	private class MyListViewClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SimpleAdapter adapter = (SimpleAdapter)lv_userList.getAdapter();
			HashMap<String, Object> map = (HashMap<String, Object>)adapter.getItem(position);
			map.put(from[1], R.drawable.radio_checked);
			String userCode = (String)map.get("userCode");
			String userName = (String)map.get("userName");
			
			EditText et_login_code = (EditText)findViewById(R.id.et_login_code);
			TextView tv_login_name = (TextView)findViewById(R.id.tv_login_name);
			et_login_code.setText(userCode);
			tv_login_name.setText(userName);
			
			if(checkedPosition != -1 && checkedPosition != position) {
				map = (HashMap<String, Object>)adapter.getItem(checkedPosition);
				map.put(from[1], R.drawable.radio_unchecked);
			}	
			
			checkedPosition = position;
			
            adapter.notifyDataSetChanged();
		}

	}

}
